Objects and Data Structures Assessment Test

Test your knowledge.

Answer the following questions

Write a brief description of all the following Object Types and Data Structures we've learned about:

Numbers: represent a whole or floating point value, pos/neg.

Strings: a list of string objects, immutable

Lists: mutable, a collection of one or more types that can be sequenced

Tuples: object representing a number of non-sequenced values

Dictionaries: key-value pairs, not indexed

Numbers

Write an equation that uses multiplication, division, an exponent, addition, and subtraction that is equal to 100.25.

Hint: This is just to test your memory of the basic arithmetic commands, work backwards from 100.25


In [1]:
((((10 + 10) * 5) / 10)**2) + 5.00 - 4.75


Out[1]:
100.25

Explain what the cell below will produce and why. Can you change it so the answer is correct?


In [3]:
# Will produce 0 in python 2. produces 0.66 in python 3 because of "true" division
2/3

# the following import will change the outcome
from __future__ import division
2/3


Out[3]:
0.6666666666666666

Answer these 3 questions without typing code. Then type code to check your answer.

What is the value of the expression 4 * (6 + 5)

What is the value of the expression 4 * 6 + 5 

What is the value of the expression 4 + 6 * 5 

In [5]:
# 4 * (6 + 5) = 44
4 * (6 + 5)


Out[5]:
44

In [6]:
# 4 * 6 + 5 = 29
4 * 6 + 5


Out[6]:
29

In [7]:
# 4 + 6 * 5 = 34
4 + 6 * 5


Out[7]:
34

What is the type of the result of the expression 3 + 1.5 + 4? Floating point number, 8.5


In [11]:
3 + 1.5 + 4


Out[11]:
8.5

What would you use to find a number’s square root, as well as its square?


In [10]:
#x**y for square, x**0.5 for square root
print(2**2)
print(4**0.5)


4
2.0

Strings

Given the string 'hello' give an index command that returns 'e'. Use the code below:


In [12]:
s = 'hello'
# Print out 'e' using indexing

# Code here
print(s[1])


e

Reverse the string 'hello' using indexing:


In [13]:
s ='hello'

# Reverse the string using indexing

# Code here
print(s[::-1])


olleh

Given the string hello, give two methods of producing the letter 'o' using indexing.


In [14]:
s ='hello'

# Print out the

# Code here
print(s[-1:])
print(s[len(s) - 1])


o
o

Lists

Build this list [0,0,0] two separate ways.


In [15]:
print([0,0,0])
print([0] * 3)


[0, 0, 0]
[0, 0, 0]

Reassign 'hello' in this nested list to say 'goodbye' item in this list:


In [16]:
l = [1,2,[3,4,'hello']]
l[2][2] = "goodbye"
print(l)


[1, 2, [3, 4, 'goodbye']]

Sort the list below:


In [22]:
l = [3,4,5,5,6]
result = l.sort()
print(result)


None

Dictionaries

Using keys and indexing, grab the 'hello' from the following dictionaries:


In [23]:
d = {'simple_key':'hello'}
# Grab 'hello'
print(d["simple_key"])


hello

In [24]:
d = {'k1':{'k2':'hello'}}
# Grab 'hello'
print(d["k1"]["k2"])


hello

In [25]:
# Getting a little tricker
d = {'k1':[ {'nest_key':['this is deep',['hello']]} ]}

#Grab hello
print(d["k1"][0]["nest_key"][1][0])


hello

In [26]:
# This will be hard and annoying!
d = {'k1':[
            1,2,{'k2':['this is tricky',{'tough':[1,2,['hello']]}
            ]}
        ]
    }

print(d["k1"][2]["k2"][1]["tough"][2][0])


hello

Can you sort a dictionary? Why or why not?

No, dictionaries aren't indexed.

Tuples

What is the major difference between tuples and lists?

mutability: lists are, tuples aren't

How do you create a tuple?


In [27]:
tup = (2, "yes", 3.0)
print(tup)


(2, 'yes', 3.0)

Sets

What is unique about a set?

non-repeating elements

Use a set to find the unique values of the list below:


In [28]:
l = [1,2,2,33,4,4,11,22,3,3,2]
s = set(l)
print(s)


set([1, 2, 3, 4, 33, 11, 22])

Booleans

For the following quiz questions, we will get a preview of comparison operators:

OperatorDescriptionExample
== If the values of two operands are equal, then the condition becomes true. (a == b) is not true.
!= If values of two operands are not equal, then condition becomes true.
<> If values of two operands are not equal, then condition becomes true. (a <> b) is true. This is similar to != operator.
> If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true.
< If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true.
>= If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true.
<= If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is true.

What will be the resulting Boolean of the following pieces of code (answer fist then check by typing it in!)


In [29]:
# Answer before running cell
2 > 3 # False


Out[29]:
False

In [30]:
# Answer before running cell
3 <= 2 # False


Out[30]:
False

In [31]:
# Answer before running cell
3 == 2.0 # False


Out[31]:
False

In [32]:
# Answer before running cell
3.0 == 3 # True


Out[32]:
True

In [33]:
# Answer before running cell
4**0.5 != 2 # False


Out[33]:
False

Final Question: What is the boolean output of the cell block below?


In [34]:
# two nested lists
l_one = [1,2,[3,4]]
l_two = [1,2,{'k1':4}]

#True or False?
l_one[2][0] >= l_two[2]['k1'] # False, 3 >= 4


Out[34]:
False

Great Job on your first assessment!